#include "Sphere.h"
#include "Common.h"

namespace Pandora
{
	int CSphere::Intersect(CRay& ray, float &t)
	{
		CVector3 dst =  centre - ray.GetStartPoint();
		float B = dst.Dot(ray.GetDirection());
		float C = dst.Dot(dst) - sqRadius;
		float D = B*B - C;
		if(D < 0.0f)
			return MISS;

		D = sqrtf(D);
		float t0 = B - D; 
		float t1 = B + D;
		int ret = MISS;
		if(t1 > 0.1f)
		{
			if(t0 < -0.1f)
			{
				if(t1 < t)
				{
					t = t1;
					ret = INSIDE; 
				}
			}
			else if(t0 > 0.1f)
			{
				if(t0 < t)
				{
					t = t0;
					ret = HIT;
				}
			}
		}

		/*if ((t0 > 0.1f) && (t0 < t)) 
		{
			t = t0;
			ret = true;
		}
		
		if ((t1 > 0.1f) && (t1 < t)) 
		{
			t = t1;
			ret = true;
		}*/
		return ret;
	}

	int CSphere::Intersect(CRay* ray, float &t)
	{
		return Intersect(*ray, t);
	}
}; //namespace Pandora